home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: malloc question
- Message-ID: <1996Mar11.184238.6959@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de>
- Date: Mon, 11 Mar 1996 18:42:03 GMT
-
- watzka@stat.uni-muenchen.de (Kurt Watzka) wrote:
-
- >alex@station.net (Alex Chu) writes:
-
- >>Hi everybody,
-
- >>I have a question for the following snip C program.
-
- >>typedef struct item {
- >> int val;
- >> struct item *next;
- >>} ITEM, *PITEM;
-
- >>main()
- >>{
- >> PITEM head, current;
- >> head=(PITEM) malloc(sizeof(ITEM));
- >> ^^^^^^^
- >> head->val=1;
- >>}
-
- >>I want to know why need to use the type casting PITEM in front of the
- >>malloc ? Please help!
-
- >Simple answer: You don't have to use that cast, and you should _not_ use
- >it, because all you can do with that cast is _hide_ an error.
-
- True. The example was missing #include <stdlib.h> which should have
- quieted his compiler about converting the return type of malloc
- (assumed int) to a pointer.
-
- >OTOH, if you are using a C++ compiler to translate C programs, the cast
- >is needed, but malloc() is obsolete.
-
- Mostly, not entirely. If you want to use realloc, you need to use
- malloc/free instead of new/delete in C++.
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@mnsinc.com (personal)
-
-